<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;


use think\Model;

class ForumNoteCateModel extends Model
{
    // 确定链接表名
    protected $name = 'forum_note_cate';
    //开启时间戳写入
    protected $autoWriteTimestamp = true;

    /**
     * 插入新的分类
     */
    public function insertCate($param)
    {
        try{
            $result =  $this->validate('ForumNoteCateValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('forum/noteCate'), '添加分类成功!');
            }
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 查询所有分类
     */
    public function getAllCates($where='',$offset='',$limit='',$order = 'sort desc')
    {
        if( empty($offset) && empty($limit) ){
            return $this->where($where)->order($order)->select();
        }
        return $this->where($where)->limit($offset, $limit)->order($order)->select();
    }

    /**
     * 根据搜索条件获取所有的分类数量
     */
    public function getAllCatesCount($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 获取单条数据信息
     */
    public function getOneCate($where)
    {
        return $this->where($where)->find();
    }

    /**
     * 编辑角色信息
     */
    public function editNoteCate($param)
    {
        try{

            $result =  $this->validate('NoteCateValidate')->save($param, ['id' => $param['id']]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('forum/noteCate'), '编辑分类成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除角色
     */
    public function delNoteCate($id)
    {
        try{
            $this->where('id', $id)->delete();
            return msg(1, '', '删除分类成功');

        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

}